/**
 * Baijiahulian.com Inc. Copyright (c) 2014-2016 All Rights Reserved.
 */
package com.baijia.passport.core;

import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @title RedisTemplate
 * @desc TODO
 * @author macrohuang
 * @date Apr 28, 2016
 * @version 1.0
 */
@Slf4j
public class RedisTemplate {

    private static JedisPool jedisPool;

    public static void setJedisPool(JedisPool jedisPool) {
        RedisTemplate.jedisPool = jedisPool;
    }

    public interface Callback<T> {
        T doInInstance(Jedis jedis);
    }

    public static <T> T execute(Callback<T> callback) {
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            return callback.doInInstance(jedis);
        } catch (Exception e) {
            log.error("Error while try to do in redis", e);
            return null;
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }
}
